const express = require('express');
// 创建路由对象
const router = express.Router();

router.post("/login", (req, res) => {
    if (req.body.username !== "admin" || req.body.password !== "000000") {
        res.send({
            status: 401,
            message: "用户名或密码错误",
        });
        return;
    }
    req.session.user = req.body;
    req.session.isLogin = true;
    res.send({
        status: 200,
        message: "登录成功",
    });
})

router.get('/getUser', (req, res) => {
    if (!req.session.isLogin) {
        return res.send({
            status: 401,
            message: "用户未登录",
        });
    }
    res.send({
        status: 200,
        message: "用户已登录",
        user: req.session.user,
    });
})

router.post('/logout', (req, res) => {
    // 清空当前客户端对应的session数据
    req.session.destroy();
    res.send({
        status: 200,
        message: "退出登录成功",
    });
})

// 向外导出路由对象
module.exports = router;